package com.rongc.client.freight.base.view.activity;

import android.support.v4.app.FragmentTabHost;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.rongc.client.core.utils.ActivityUtils;
import com.rongc.client.freight.R;
import com.rongc.client.freight.base.BaseActivity;
import com.rongc.client.freight.base.view.fragment.HomeFragment;
import com.rongc.client.freight.base.view.fragment.MessageFragment;
import com.rongc.client.freight.base.view.fragment.MineFragment;

/**
 * Created by ccqx on 2017/6/29.
 */

public class HostActivity extends BaseActivity{
    private FragmentTabHost mTabHost;
    private LayoutInflater layoutInflater;
    private Class fragmentArray[] = {HomeFragment.class,MessageFragment.class,MineFragment.class};
    private int mImageViewArray[] = {R.drawable.host_bottom_1,R.drawable.host_bottom_2,R.drawable.host_bottom_3};
    private int mTextviewArray[] = {R.string.host_tab_main,R.string.host_tab_message,R.string.host_tab_mine};

    @Override
    public void initView() {
        layoutInflater = LayoutInflater.from(this);
        mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
        int count = fragmentArray.length;
        for(int i = 0; i < count; i++){
            TabHost.TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i]+"").setIndicator(getTabItemView(i));
            mTabHost.addTab(tabSpec, fragmentArray[i], null);
        }
    }

    /**
     * 给Tab按钮设置图标和文字
     */
    private View getTabItemView(int index){
        View view = layoutInflater.inflate(R.layout.widget_host_tab, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.tab_iv_image);
        TextView mTxtTitle = (TextView) view.findViewById(R.id.tab_tv_content);
        imageView.setImageResource(mImageViewArray[index]);
        mTxtTitle.setText(mTextviewArray[index]);
        return view;
    }

    private long exitTime = 0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {

            if ((System.currentTimeMillis() - exitTime) > 2000) {
                ActivityUtils.toast( "再按一次返回退出应用");
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                System.gc();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_host;
    }
}
